﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
using Microsoft.Linq.Expressions;

namespace DlrBasic.Parser.Ast
{
    class NegateOperator : DlrBasicExpression
    {

        DlrBasicExpression expression;

        public NegateOperator(SourceSpan span, DlrBasicExpression expression)
            : base(span)
        {
            this.expression = expression;
        }

        protected internal override Expression Generate(DlrBasicGenerator tg)
        {
            Expression operand = expression.Generate(tg);

            System.Reflection.MethodInfo method;
            Expression converted;

            TypeConversionUtils.FindMethodAndConvertOperands(typeof(Runtime.DlrBasicMath),
                "Negate", operand, out method, out converted);

            return Expression.Call(method, operand);
        }

        protected internal override DlrBasicExpression GetSingleLeaf(DlrBasicGenerator generator)
        {
            throw new NotSupportedException();
        }
    }


    class NotOperator : DlrBasicExpression
    {
        DlrBasicExpression expression;

        public NotOperator(SourceSpan span, DlrBasicExpression expression)
            : base(span)
        {
            this.expression = expression;
        }

        protected internal override Expression Generate(DlrBasicGenerator tg)
        {
            Expression operand = expression.Generate(tg);

            System.Reflection.MethodInfo method;
            Expression converted;

            TypeConversionUtils.FindMethodAndConvertOperands(typeof(Runtime.DlrBasicMath),
                "Not", operand, out method, out converted);

            return Expression.Call(method, operand);
        }

        protected internal override DlrBasicExpression GetSingleLeaf(DlrBasicGenerator generator)
        {
            throw new NotSupportedException();
        }
    }
}
